package meatslope2.gameengine.input;

import meatslope2.gameengine.engine.Engine;
import android.view.KeyEvent;
import android.view.MotionEvent;

//TODO: might be better to make an aggregate model with a keyboard system and touch system, but right now we're
//relaying them to the game to handle the logic anyway, look at this further in development
//
//Update: definitely will - trackball, keyboard, virtual pad, tilt
//Update: piss on that, only plan on supporting multi-touch systems - will investigate later
public class InputSystem 
{
	private final Engine engine;
	
	public InputSystem(Engine engine)
	{
		this.engine = engine;
	}
	
    public boolean onKeyDown(int keyCode, KeyEvent event) 
    {
    	boolean result = true;
    	if (keyCode == KeyEvent.KEYCODE_BACK) 
    	{
			//Would you like to quit the game?
			result = true;
    	}
    	else if (keyCode == KeyEvent.KEYCODE_MENU)
    	{
    		//Show game menu
    		result = true;
    	} 
    	else 
    	{
		    result = engine.onKeyDownEvent(keyCode);
		    
		    // Sleep so that the main thread doesn't get flooded with UI events.
		    //TODO: run tests, see if we need this
		    try 
		    {
		        Thread.sleep(100); //4
		    } 
		    catch (InterruptedException e) 
		    {
		        // No big deal if this sleep is interrupted.
		    }
    	}
    	
        return result;
    }
     
    public boolean onKeyUp(int keyCode, KeyEvent event) 
    {
    	boolean result = false;
    	if (keyCode == KeyEvent.KEYCODE_BACK) 
    	{
    		result = true;
    	} 
    	else 
    	{
    		result = engine.onKeyUpEvent(keyCode);
	        // Sleep so that the main thread doesn't get flooded with UI events.
    		//TODO: run tests, see if we need this
	        try 
	        {
	            Thread.sleep(100); //4
	        } 
	        catch (InterruptedException e) 
	        {
	            // No big deal if this sleep is interrupted.
	        }
    	}
    	
        return result;
    }

	public boolean onTouchEvent(MotionEvent event) 
	{
		return engine.onTouchEvent(event);
	}
}
